package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.iterator.SaturnStartIteratorDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnStartIterationNode extends Shape {

	public static final String KEY_NODE_STAETITERATION = "start-iteration";

	public static final String KEY_PROPERTY_STAETITERATION_NODE_NAME = "_property_name";

	public static final String KEY_PROPERTY_STAETITERATION_NODE_DESCRITPION = "_property_description";

	public static final String KEY_PROPERTY_STAETITERATION_NODE_LIST_KEY = "_property_iterator-list-key";

	public static final String KEY_PROPERTY_STAETITERATION_NODE_ITEM_KEY = "_property_item-key";

	public static final String KEY_PROPERTY_STAETITERATION_NODE_END_ID = "_property_end-id";

	public static final String SHOW_STAETITERATION_NODE_NAME = "name";

	public static final String SHOW_STAETITERATION_NODE_DESCRITPION = "description";

	public static final String SHOW_STAETITERATION_NODE_LIST_KEY = "iterator-list-key";

	public static final String SHOW_STAETITERATION_NODE_ITEM_KEY = "item-key";

	public static final String SHOW_STAETITERATION_NODE_END_ID = "end-id";

	private static final long serialVersionUID = 8049159744627367228L;

	private static int i = 1;
	
	private String listkey = "";

	private String itemkey = "";

	private String endid = "";

	private Image image = Activator.getImage(Images.FOR_START_ICON);

	public SaturnStartIterationNode() {
		super.setName(Messages.getString("SaturnStartIterationNode.StartLoop")+ (i++)); //$NON-NLS-1$
	}

	@Override
	public Image getIcon() {
		return image;
	}

	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	public String getListkey() {
		return listkey;
	}

	public void setListkey(String listkey) {
		if (!this.listkey.equals(listkey)) {
			this.listkey = listkey;
			firePropertyChange(PROPERTERY_CHANGS, null, listkey);
		}
	}

	public String getItemkey() {
		return itemkey;
	}

	public void setItemkey(String itemkey) {

		if (!this.itemkey.equals(itemkey)) {
			this.itemkey = itemkey;
			firePropertyChange(PROPERTERY_CHANGS, null, itemkey);
		}

	}

	public String getEndid() {
		return endid;
	}

	public void setEndid(String endid) {
		if (!this.endid.equals(endid)) {
			this.endid = endid;
			firePropertyChange(PROPERTERY_CHANGS, null, endid);
		}
	}

	public void setImage(Image image) {
		this.image = image;
	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node startIterationNode = SaturnW3CUtil.createChildNode(root,
				KEY_NODE_STAETITERATION, null);
		
		SaturnW3CUtil.createChildNode(startIterationNode,
				SHOW_STAETITERATION_NODE_NAME, super.getName());

		SaturnW3CUtil.createChildNode(startIterationNode,
				SHOW_STAETITERATION_NODE_LIST_KEY, this.listkey);

		SaturnW3CUtil.createChildNode(startIterationNode,
				SHOW_STAETITERATION_NODE_ITEM_KEY, this.itemkey);

		SaturnW3CUtil.createChildNode(startIterationNode,
				SHOW_STAETITERATION_NODE_END_ID, this.endid);

		return startIterationNode;
	}

	@Override
	public Dialog getDialog() {
		return new SaturnStartIteratorDialog(super.getShell(), this);
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnStartIterationNode saturnStartIterationNode = new SaturnStartIterationNode();
		SaturnStartIterationNode saturn = (SaturnStartIterationNode) shape;
		saturnStartIterationNode.setLocation(mousePoint);
		saturnStartIterationNode.setName(saturn.getName());
		saturnStartIterationNode.setDescription(saturn.getDescription());
		saturnStartIterationNode.setListkey(saturn.getListkey());
		saturnStartIterationNode.setItemkey(saturn.getItemkey());
		saturnStartIterationNode.setEndid(saturn.getEndid());
		saturnStartIterationNode.setId(saturn.getId());
		saturnStartIterationNode.setNextId(saturn.getNextId());
		return saturnStartIterationNode;
	}
	
	public String[] getEndIteratorIds() {
		List<String> ids = new ArrayList<String>();
		
		List<Shape> shapes = super.getAllShapes();
		
		if (shapes != null) {
			for (Shape shape : shapes) {
				if (shape instanceof SaturnEndIterationNode) {
					ids.add(shape.getName() + "[" + shape.getId() + "]");
				}
			}
		}
		
		return ids.toArray(new String[ids.size()]);
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();
		
		infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("value", getListkey());
	 	infos.put("iterationItem", getItemkey());
	 	infos.put("endIterationNode", getEndid());
	 	infos.put("description", getDescription());
		
		para.setInfourl( name + "-start-iteration.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.IteratorStartNode")); //$NON-NLS-1$
		para.setImagename("start-iteration");
		String templatePath = "template/export/logic/startiteration";
		String Path = filePath + File.separator + name + "-start-iteration.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,infos, Path);
		
		return para;
	}
}